import { _decorator, Collider2D, Component, Contact2DType, director, Input, input, instantiate, Label, Node, Prefab, tween, Vec3,Animation, AudioClip, AudioSource, sys } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {

    @property(Node)
    Target_Note: Node = null

    @property(Prefab) // 导入剑的预制体
    Arrow_Prefab:Prefab = null

    @property(Node)
    Arrow_Top_Note:Node = null // 导入剑的父节点

    @property(Node)
    Tips_Note:Node = null

    @property(Label)
    Tips_Label:Label = null

    @property(Animation)
    Button_Animation:Animation = null

    @property(AudioClip) // 射击音频
    Win_AudioClip:AudioClip = null

    @property(AudioClip) // 失败音频
    Fail_AudioClip:AudioClip = null

    @property(AudioClip) // 过关音频
    Success_AudioClip:AudioClip = null

    @property(Node)
    Bj_Note:Node = null

    Angle = 0 // 初始旋转角度

    Angle_Speed = 50 // 旋转速度

    Arrow_Node: Node = null // 箭自身节点

    Move = true

    @property(Label)
    Total_Num:Label = null

    @property(Label)
    Success_Num:Label = null

    Game_Total = 5
    Game_Ok_Total = 0

    protected onLoad(): void {
        input.on(Input.EventType.TOUCH_START,this.TOUCH_START,this)

        this.Bj_Note.getComponent(AudioSource).volume = 0.3

        this.Game_Total = sys.localStorage.getItem("Game_Total") || 5

        this.Total_Num.string = this.Game_Total+""
        this.Success_Num.string = "0"
    }


    protected onDestroy(): void {
        input.off(Input.EventType.TOUCH_START,this.TOUCH_START,this)
    }


    restart(){
        // this.Tips_Note.active = false
        director.loadScene("main")
    }

    Success_Play(){ // 播放成功音效

        this.node.getComponent(AudioSource).playOneShot(this.Success_AudioClip,1)

    }
    Win_Play(){ // 播放成功音效

        this.node.getComponent(AudioSource).playOneShot(this.Win_AudioClip,1)
    }

    Fail_Play(){ // 播放成功音效

        this.node.getComponent(AudioSource).playOneShot(this.Fail_AudioClip,1)

    }

    Bj_Stop(){
        this.Bj_Note.getComponent(AudioSource).stop()

    }

    TOUCH_START(){

        const Arrow_Node = instantiate(this.Arrow_Prefab) // 预制体生成为节点
        Arrow_Node.setParent(this.Arrow_Top_Note)


        Arrow_Node.getComponent(Collider2D).on(Contact2DType.BEGIN_CONTACT,this.BEGIN_CONTACT,this)

        tween(Arrow_Node).to(0.1,{position:new Vec3(0,100,0)}).call(()=>{

            this.Arrow_To_Target(Arrow_Node)

            Arrow_Node.getComponent(Collider2D).off(Contact2DType.BEGIN_CONTACT,this.BEGIN_CONTACT,this)

        }).start();
      
    }
    BEGIN_CONTACT(){
        this.tips(false)
    }

    tips(success){
        input.off(Input.EventType.TOUCH_START,this.TOUCH_START,this)
        this.Tips_Note.active = true
        this.Move =false
        this.Button_Animation.play("d")
        if(success){        
            this.Success_Play()  

            sys.localStorage.setItem("Game_Total",Number(this.Game_Total)+5)

            this.Tips_Label.string = "挑战成功"
        }else{
            this.Fail_Play()
            this.Tips_Label.string = "挑战失败了"
            this.Bj_Stop()
             sys.localStorage.setItem("Game_Total",5)
        }
          
    }

    Arrow_To_Target(Arrow_Node)  {

        this.Win_Play()

        const world_pos = Arrow_Node.getWorldPosition(); // 获取世界坐标

        Arrow_Node.setParent(this.Target_Note)

        Arrow_Node.setWorldPosition(world_pos)
        

        Arrow_Node.angle = -this.Angle

        this.Game_Ok_Total ++

        this.Success_Num.string =  this.Game_Ok_Total+""
         
        if(this.Game_Ok_Total >= this.Game_Total){
           this.tips(true)
        }
        

    }

    start() {

    }

    update(deltaTime: number) {

        if(!this.Move) return
        if(this.Angle >=360){
            this.Angle = 0;
        }
        this.Angle +=  deltaTime * this.Angle_Speed

        this.Target_Note.angle = this.Angle
    }
}


